package com.ph.irp.base.bom.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ph.exception.PHException;
import com.ph.irp.base.bom.dao.BOMBillDAO;
import com.ph.irp.base.bom.dao.BOMBillDetailDAO;
import com.ph.irp.base.bom.dao.BOMGroupDAO;
import com.ph.irp.base.bom.service.BOMManageService;
import com.ph.irp.base.materialassistantattribute.dao.AssistantAttributeDao;
import com.ph.irp.base.measure.dao.PBaseMeasureUnitDAO;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectMaterialDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectStorageDao;
import com.ph.irp.dto.BOMBillDetailDTO;
import com.ph.irp.dto.BOMGroupDTO;
import com.ph.irp.dto.BomBillDTO;
import com.ph.irp.model.PBaseBombill;
import com.ph.irp.model.PBaseBombilldetail;
import com.ph.irp.model.PBaseBomgroup;
import com.ph.irp.model.PBaseMaterialAssistantattribute;
import com.ph.irp.model.PBaseMeasureunit;
import com.ph.irp.model.PBaseVerifyprojectMaterial;
import com.ph.irp.model.PBaseVerifyprojectStorage;
import com.ph.util.DateUtil;

public class BOMManageServiceImpl implements BOMManageService {
	BOMGroupDAO bomGroupDaoProxy;
	BOMBillDAO bomBillDaoProxy;
	BOMBillDetailDAO bomBillDetailDaoProxy;
	PBaseVerifyprojectMaterialDao materialDao;
	PBaseVerifyprojectStorageDao storageDao;
	PBaseMeasureUnitDAO measureUnitDao;
	AssistantAttributeDao attributeDao;

	public void setMeasureUnitDao(PBaseMeasureUnitDAO measureUnitDao) {
		this.measureUnitDao = measureUnitDao;
	}

	public void setAttributeDao(AssistantAttributeDao attributeDao) {
		this.attributeDao = attributeDao;
	}

	public void setStorageDao(PBaseVerifyprojectStorageDao storageDao) {
		this.storageDao = storageDao;
	}

	public void setMaterialDao(PBaseVerifyprojectMaterialDao materialDao) {
		this.materialDao = materialDao;
	}

	public void setBomGroupDaoProxy(BOMGroupDAO bomGroupDaoProxy) {
		this.bomGroupDaoProxy = bomGroupDaoProxy;
	}

	public void setBomBillDaoProxy(BOMBillDAO bomBillDaoProxy) {
		this.bomBillDaoProxy = bomBillDaoProxy;
	}

	public void setBomBillDetailDaoProxy(BOMBillDetailDAO bomBillDetailDaoProxy) {
		this.bomBillDetailDaoProxy = bomBillDetailDaoProxy;
	}

	public List<BOMGroupDTO> getAllBOMGroup() throws Throwable {
		
			return convertDTOList(bomGroupDaoProxy.getAllBOMGroup());
		
	}

	public List<BOMGroupDTO> getAllBOMGroupExcludeBOMGroupID(
			String editBOMGroupID) throws Throwable {
		
			return convertDTOList(bomGroupDaoProxy
					.getAllBOMGroupExcludeBOMGroupID(editBOMGroupID));
		
	}

	public void addBOMGroup(String bomGroupCode, String bomGroupName,
			String bomGroupParentID) throws Throwable {
		
			bomGroupDaoProxy.addBOMGroup(bomGroupCode, bomGroupName,
					bomGroupParentID);
		
	}

	public void addBOMBill(PBaseBombill bomBill) throws Throwable {
		
			bomBillDaoProxy.addBOMBill(bomBill);
		

	}

	public List<BOMGroupDTO> getBOMGroupByCodeAndName(String code, String name)
			throws Throwable {
		return convertDTOList(bomGroupDaoProxy.getBOMGroupByCodeAndName(code, name));
	}

	private List<BOMGroupDTO> convertDTOList(List<PBaseBomgroup> bomGroupList)
			throws Throwable {
		List<BOMGroupDTO> resList = new ArrayList<BOMGroupDTO>();
		for (PBaseBomgroup group : bomGroupList) {
			BOMGroupDTO gDTO = new BOMGroupDTO();
			String bomParentGroupName = group.getParentId() == 0 ? ""
					: bomGroupDaoProxy.getBOMGroupNameByBOMGroupID(group
							.getParentId());
			gDTO.setBomGroupCode(group.getBomgroupCode());
			gDTO.setBomGroupName(group.getBomgroupName());
			gDTO.setBomParentGroupName(bomParentGroupName);
			gDTO.setBomGroupID(String.valueOf(group.getBomgroupId()));
			resList.add(gDTO);
		}

		return resList;
	}

	public PBaseBomgroup getBOMGroupByBOMGroupId(String BOMGroupID)
			throws Throwable {

		return bomGroupDaoProxy.getBOMGroupByBOMGroupId(BOMGroupID);

	}

	public void editBOMGroup(PBaseBomgroup editBOMGroup) throws Throwable {

		bomGroupDaoProxy.editBOMGroup(editBOMGroup);

	}

	public void deleteBOMGroup(String deletedBOMGroupID) throws Throwable {
		String[] deleteBOMGroupIDArray = deletedBOMGroupID.split(",");

		for (String bomGroupID : deleteBOMGroupIDArray) {
			if (bomGroupDaoProxy.getBOMGroupByBOMParentGroupId(
					Integer.parseInt(bomGroupID)).size() != 0) {
				throw new PHException("有下级BOM组的BOM组不能被删除");
			} else {
				bomGroupDaoProxy.deleteBOMGroupByBOMGroupID(bomGroupID);
			}
		}
	}

	public void deleteBOMBill(String deleteBOMBillIDs) throws Throwable {
		if (deleteBOMBillIDs != null && !deleteBOMBillIDs.equals("")) {
			bomBillDaoProxy.deleteBOMBill(deleteBOMBillIDs);
		}

	}

	public List<BomBillDTO> getBOMBillByBOMGroupID(String bomGroupID)
			throws Throwable {

		List<PBaseBombill> bomBillList = bomBillDaoProxy
				.getBOMBillByBOMGroupID(bomGroupID);

		return convertBombillDTOList(bomBillList);
	}

	public BomBillDTO getBOMBillByBOMBillID(String bomBillID) throws Throwable {
		PBaseBombill bomBill = bomBillDaoProxy.getBOMBillByBOMBillID(bomBillID);

		return convertBombillDTO(bomBill);
	}

	public List<BOMBillDetailDTO> getBOMBillDetailListByBOMBillID(
			String bomBillID) throws Throwable {
		List<PBaseBombilldetail> bomBillDetailList = bomBillDetailDaoProxy
				.getBOMBillDetailListByBOMBillID(bomBillID);

		return convertBombillDetailDTOList(bomBillDetailList);
	}

	private List<BOMBillDetailDTO> convertBombillDetailDTOList(
			List<PBaseBombilldetail> bomBillDetailList) throws Throwable {
		List<BOMBillDetailDTO> resList = new ArrayList<BOMBillDetailDTO>();

		for (PBaseBombilldetail bomBillDetail : bomBillDetailList) {
			resList.add(convertBombillDetailDTO(bomBillDetail));
		}

		return resList;
	}

	private List<BomBillDTO> convertBombillDTOList(
			List<PBaseBombill> bomBillList) throws Throwable {
		List<BomBillDTO> resList = new ArrayList<BomBillDTO>();

		for (PBaseBombill bomBill : bomBillList) {
			resList.add(convertBombillDTO(bomBill));
		}

		return resList;
	}

	private BOMBillDetailDTO convertBombillDetailDTO(
			PBaseBombilldetail bomBillDetail) throws Throwable {
		BOMBillDetailDTO bomBillDetailDTO = new BOMBillDetailDTO();

		PBaseVerifyprojectMaterial material = materialDao
				.getMaterialById(bomBillDetail
						.getPBaseVerifyprojectMaterialId());
		PBaseBombill bomBill = bomBillDaoProxy.getBOMBillByBOMBillID(String
				.valueOf(bomBillDetail.getPBaseBombillId()));
		PBaseVerifyprojectStorage storage = storageDao
				.getStorageById(bomBillDetail.getPBaseVerifyprojectStorageId());
		PBaseMeasureunit measureUnit = new PBaseMeasureunit();
		measureUnit.setMeasureGroupId(material.getPBaseMeasuregroupId());
		String basicMeausreUnitName = null;
		if (measureUnitDao.getMeasureUnitByGroupId(measureUnit).size() != 0) {
			basicMeausreUnitName = measureUnitDao.getMeasureUnitByGroupId(
					measureUnit).get(0).getMeasureUnitName();
		}

		String meausreUnitName = measureUnitDao.getMeasureUnitById(
				bomBillDetail.getPBaseMeasureUnitId()).getMeasureUnitName();

		String assistantAttributeName = null;
		if (bomBillDetail.getPBaseMaterialAssistantattributeId() != 0) {
			PBaseMaterialAssistantattribute attribute = attributeDao
					.getAssistantAttributeById(bomBillDetail
							.getPBaseMaterialAssistantattributeId());
			assistantAttributeName = attribute.getAssistantAttributeName();
		} else {
			assistantAttributeName = "";
		}
		// 0为基本类，1为组合类
		bomBillDetailDTO.setStorageId(String.valueOf(storage.getStorageId()));
		bomBillDetailDTO
				.setMaterialId(String.valueOf(material.getMaterialId()));
		bomBillDetailDTO.setMaterialCode(material.getMaterialCode());
		bomBillDetailDTO.setMaterialName(material.getMaterialName());
		bomBillDetailDTO.setSpec(material.getSpec());
		bomBillDetailDTO.setMaterialAttribute(material.getMaterialAttribute());
		bomBillDetailDTO.setAssistantAttribute(assistantAttributeName);
		bomBillDetailDTO.setMeasureUnitName(meausreUnitName);
		bomBillDetailDTO.setBasicMeasureUnitName(basicMeausreUnitName);
		bomBillDetailDTO.setBasicCost("1.00");
		bomBillDetailDTO.setCost(String.valueOf(bomBillDetail.getUsedNumber()));
		bomBillDetailDTO.setLossPercent(String.valueOf(bomBillDetail
				.getLossPercent()));
		bomBillDetailDTO.setUsedState(bomBill.getState() != 0 ? "使用" : "未使用");
		bomBillDetailDTO.setStorageName(storage.getStorageName());
		bomBillDetailDTO.setMeasureUnitId(String.valueOf(bomBillDetail.getPBaseMeasureUnitId()));
	
		return bomBillDetailDTO;
	}

	private BomBillDTO convertBombillDTO(PBaseBombill bomBill) throws Throwable {
		BomBillDTO bomBillDTO = new BomBillDTO();
		PBaseVerifyprojectMaterial material = materialDao
				.getMaterialById(bomBill.getVerifyProjectMaterialId());
		// 0为基本类，1为组合类
		bomBillDTO
				.setAttributeBasicType(bomBill.getAttributeBasicType() != 0 ? "组合类"
						: "基本类");
		// 0为未审核，1为已审核
		bomBillDTO.setAuditState(bomBill.getAuditState() != 0 ? "已审核" : "未审核");
		bomBillDTO.setBomBillCode(bomBill.getBomBillCode());
		bomBillDTO.setBomBillId(bomBill.getBomBillId());
		bomBillDTO.setBomGroupId(bomBill.getBomGroupId());
		bomBillDTO.setBomGroupName(bomGroupDaoProxy
				.getBOMGroupNameByBOMGroupID(bomBill.getBomGroupId()));
		bomBillDTO.setFee(bomBill.getFee());
		bomBillDTO.setMemo(bomBill.getMemo());
		bomBillDTO.setNumber(bomBill.getNumber());
		// 0为未使用，1为使用
		bomBillDTO.setState(bomBill.getState() != 0 ? "使用" : "未使用");
		bomBillDTO.setUnit(bomBill.getUnit());
		bomBillDTO.setVerifyProjectMaterialCode(material.getMaterialCode());
		bomBillDTO.setVerifyProjectMaterialId(material.getMaterialId());
		bomBillDTO.setVerifyProjectMaterialMaterialAttribute(material
				.getMaterialAttribute());
		bomBillDTO.setVerifyProjectMaterialName(material.getMaterialName());
		bomBillDTO.setVerifyProjectMaterialSpec(material.getSpec());
		bomBillDTO.setBillAuditerName(bomBill.getAuditer());
		bomBillDTO.setBillAuditDate(DateUtil.time2String(
				bomBill.getAuditTime(), DateUtil.YMD));
		return bomBillDTO;
	}

	public List<BomBillDTO> getBOMBillByStateAndCode(String bomBillCode,
			String state, String auditState, String bomGroupID)
			throws Throwable {
		List<PBaseBombill> bomBillList = bomBillDaoProxy.getBOMBillByStateAndCode(
				bomBillCode, state, auditState, bomGroupID);

		return convertBombillDTOList(bomBillList);
	}

	public int getAuditStateByBOMBillID(String useBomBillID) throws Throwable {
		return bomBillDaoProxy.getAuditStateByBOMBillID(useBomBillID);
	}

	public int getStateByBOMBillID(String useBomBillID) throws Throwable {
		return bomBillDaoProxy.getStateByBOMBillID(useBomBillID);
	}

	public void setBomBillUsed(String useBomBillID) throws Throwable {
		bomBillDaoProxy.setBomBillUsed(useBomBillID);
	}

	public void setBomBillAntiUsed(String useBomBillID) throws Throwable {
		bomBillDaoProxy.setBomBillAntiUsed(useBomBillID);
	}

	public void setBomBillAudited(String useBomBillID, String auditer,
			String auditDate) throws Throwable {
		bomBillDaoProxy.setBomBillAudited(useBomBillID, auditer, auditDate);
	}

	public void setBomBillAntiAudited(String useBomBillID) throws Throwable {
		bomBillDaoProxy.setBomBillAntiAudited(useBomBillID);
	}

	public String getNewestBomBillCode() throws Throwable {
		return bomBillDaoProxy.getNewestBomBillCode();
	}

	public int getBOMBillIdByBOMBillCode(String code) throws Throwable {
		return bomBillDaoProxy.getBOMBillIdByBOMBillCode(code);
	}

	public void addBOMBillDetail(PBaseBombilldetail bomBillDatail)
			throws Throwable {
		bomBillDetailDaoProxy.addBOMBillDetail(bomBillDatail);
	}

	public void editBOMBill(PBaseBombill editedBOMBill) throws Throwable {
		bomBillDaoProxy.editBOMBill(editedBOMBill);
	}

	public void deleteBOMBillDetailByBOMBillID(String bomBillID)
			throws Throwable {
		bomBillDetailDaoProxy.deleteBOMBillDetailByBOMBillID(bomBillID);
	}

	public boolean isAuditedBOMBill(String bomBillID) throws Throwable {
		return bomBillDaoProxy.isAuditedBOMBill(bomBillID);
	}
}
